#!/usr/bin/python
'''
/*
 * This computer program is the confidential information and proprietary trade
 * secret of VistaraIT LLC.  Possessions and use of this program must  conform
 * strictly to the license agreement between the user and VistaraIT LLC.,  and
 * receipt or possession does not convey any rights to divulge, reproduce,  or
 * allow others to use this program without specific written authorization  of
 * VistaraIT LLC.
 * 
 * Copyright (c) 2014 VistaraIT, Inc. All rights reserved.
 */
'''

from appliance.configmanager import ConfigManager
import os

class RequestUtils():
    
    def __init__(self,request):
        self.request = request
        self.webutils = WebUtils()
        
    def getParameter(self,name,default=None):
        return self.request.POST.get(name,default)
       
    
    def getBooleanParameter(self,name):
        value = self.getParameter(name, False);
        return self.webutils.getBooleanValue(value)
    
    def getInterger(self,param):
        try:
            return int(self.getParameter(param, 0))
        except:
            return 0

class SessionUtils():
    
    def __init__(self,request):
        self.request = request
    
    def getAttribute(self,key):
        try:
            return self.request.session[key]
        except KeyError:
            pass
        return None
    
    def setAttribute(self,key,value):
        self.request.session[key] = value

class WebUtils(object):
    
    def __init__(self):
        self.sessionFile = ConfigManager().getProperty(ConfigManager().SESSION_FILE)
        
    def isNULL(self,val):
        value = str(val)
        try:
            if value == None or value.strip() == '':
                return True
        except:
            return True
        return False
    
    def getBooleanValue(self,value):
        strval = str(value).lower()
        if strval == "true" or strval == "on":
            return True
        elif strval == "false" or strval == "off":
            return False
        return None
    
    def destroyActiveSession(self):
        if os.path.exists(self.sessionFile):
            os.remove(self.sessionFile)
        
    def getActiveSessionID(self):
        if os.path.exists(self.sessionFile):
            file = open(self.sessionFile,'r')
            sk = file.readlines()
            file.close()
            return sk[0].strip()
        return None
    
    def getActiveUserDetails(self):
        if os.path.exists(self.sessionFile):
            file = open(self.sessionFile,'r')
            sk = file.readlines()
            file.close()
            itemCount = len(sk)
            if itemCount > 1:
                return sk[1].strip(),sk[2].strip()
            else:
                return None
        return None
    
    def makeActiveSession(self,sessionRequest):
        file = open(self.sessionFile,'w')
        file.write(sessionRequest.session.session_key + "\n")
        try:
            userIP = sessionRequest.META['REMOTE_ADDR']
            file.write(userIP + "\n")
            file.write('A')
        except:
            pass
        file.close()
        
    def isActiveSession(self,current_skey):
        return self.getActiveSessionID() == current_skey;
